package com.soul.doc.utils;



import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigDecimal;

public class FileUtil {
    /**
     *  TYPE_0:小时
     *  TYPE_1:分钟
     *  TYPE_2:秒钟
     *  TYPE_3:毫秒
     */
    public final static int TYPE_0 = 0;
    public final static int TYPE_1 = 1;
    public final static int TYPE_2 = 2;
    public final static int TYPE_3 = 3;
    /**
     * @Author: sws
     * @return:
     * @Description: 获取音乐的时长，返回秒数
     */
    public static Integer getMusicLength(MultipartFile file){
//        file.
        try {
            String path = getFilePath() + file.getOriginalFilename();
            File source = new File(path);
            file.transferTo(source);
            BigDecimal duration = FfmpegCmdDurationTest.duration("-i " + path, TYPE_2);
            return duration.intValue();
        } catch(Exception e) {
            return 0;
        }
    }



    /**
     * 获取路径
     *
     * @return
     * @throws Exception
     */
    private static String getFilePath() throws Exception {
        //获取当前文件的根路径
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        if (!path.exists()) path = new File("");

        //盘符路径
        StringBuilder codeUrl = new StringBuilder();
        codeUrl.append(path.getAbsolutePath()).append("/static/video/");
        File file = new File(codeUrl.toString());
        if (!file.exists()) {
            file.mkdirs();
        }
        return codeUrl.toString();
    }
}
